<?php

/******************************************************
 *					 	SESSION						  *
 ******************************************************/

/**
 * Starts global SESSION
 *
 * @author Pablo
 * @since 1.0
 */
function blg_session_start(){
	if(!blg_session_is_started()){	
		session_start();
	}
	
	//Message stack
	if(!blg_session_isset('messages'))
		$_SESSION['messages']= new blgStack();

	if(!isset($_SESSION['temp']))
		$_SESSION['temp']= array();
}

/**
 * Asks if SESSION is alredy started
 * @return bool This function returns true if a session is already started, otherwise false
 * @author Pablo
 * @since 1.0
 */
function blg_session_is_started(){
	return isset($_SESSION); 
}

/**
 * Gets a SESSION attribute
 * @param string $name
 * @return mixed The retreived data
 * @author Pablo
 * @since 1.0
 */
function blg_session_get($name){	
	if(!blg_session_is_started())
		blg_session_start();
		
	if(isset($_SESSION[$name]))
		return $_SESSION[$name];
		
	if(isset($_SESSION['temp'][$name])){
		$value= $_SESSION['temp'][$name];
		return $value;
	}
		
	return NULL;
}

/**
 * Sets a SESSION attribute
 * @param $name
 * @param $value
 * @author Pablo
 * @since 1.0
 */
function blg_session_set($name, $value, $temp=FALSE){
	if(!blg_session_is_started()) 
		blg_session_start();	

	if($temp===FALSE)
		$_SESSION[$name]= $value;
	else{
		$_SESSION['temp'][$name]= $value;		
	}
}


function blg_session_isset($name){
	if(!blg_session_is_started())
		return FALSE;

	return (isset($_SESSION[$name]) || isset($_SESSION['temp'][$name]));
}

/**
 * Unset a SESSION attribute
 * @param $name
 * @author Pablo
 * @since 1.0
 */
function blg_session_unset($name){
	blg_session_set($name, NULL, FALSE);
	blg_session_set($name, NULL, TRUE);
}

/**
 * Destroy all data registed to the session
 * @author Pablo
 * @since 1.0
 */
function blg_session_destroy($temp=FALSE){
	if($temp!==FALSE)
		unset($_SESSION['temp']);
	else
		unset($_SESSION);
}


/******************************************************
 *					   MESSAGES						  *
 ******************************************************/

/**
 * Add a message to display 
 * @param $msg
 * @param $error
 * @author Pablo
 * @since 1.0
 */
function blg_msg_add($msg, $error=FALSE){
	$_SESSION['messages']->push(array($msg, $error));	
}

/**
 * Returns oldest message array
 * @return Array
 * @author Pablo
 * @since 1.0 
 */
function blg_msg_get(){	
	if($_SESSION['messages']->is_empty())
		return FALSE;

	return $_SESSION['messages']->shift();
}

/**
 * Returns an order array with all messages to print
 * @return Array
 * @author Pablo
 * @since 1.0 
 */
function blg_msg_get_all(){
	return $_SESSION['messages']->extract_all();
}


/******************************************************
 *					   FOCUS						  *
 ******************************************************/
function blg_focus($field){
	if(!empty($field))
		blg_session_set('focus_on_load_id', $$field, TRUE);
}
